//
//  UIView+Network.swift
//  Extend
//
//  Created by 元穗 on 2017/6/6.
//  Copyright © 2017年 敬信. All rights reserved.
//

import UIKit
import Kingfisher

public extension UIImageView {

    struct Keys {
        static fileprivate var url: String = String();
        static fileprivate var renderer: String = String();
        static fileprivate var interceptors: String = String();
        static fileprivate var placeHolder: String = String();
    }

    fileprivate var url: String {
        get {
            return associated(to: self, key: &Keys.url) {
                String()
            };
        }
        set {
            associate(to: self, key: &Keys.url, value: newValue);
        }
    }

    private var renderer: Renderer {
        get {
            return associated(to: self, key: &Keys.renderer) {
                self.renderer;
            };
        }
        set {
            associate(to: self, key: &Keys.renderer, value: newValue);
        }
    }

    private var interceptors: Array<Interceptor> {
        get {
            return associated(to: self, key: &Keys.interceptors) {
                Array()
            };
        }
        set {
            associate(to: self, key: &Keys.interceptors, value: newValue);
        }
    }
    
    private var placeHolder: String {
        get {
            return associated(to: self, key: &Keys.placeHolder) {
                String()
            };
        }
        set {
            associate(to: self, key: &Keys.placeHolder, value: newValue)
        }
    }


    public func url(_ u: String) -> Self {
        url = u;
        return self;
    }

    public func renderer(_ r: Renderer) -> Self {
        renderer = r;
        return self;
    }

    public func placeHolder(_ image: String) -> Self {
        return self;
    }

    public func interceptor(i: Interceptor) -> Self {
        interceptors.append(i);
        return self;
    }

    public func load() {
        if self.url .isEmpty {
            return;
        }
        
        if (self.renderer is CircleRenderer) {
            let circle = self.renderer as! CircleRenderer;
            self.layer.borderWidth = CGFloat(circle.borderWidth);
            self.layer.borderColor = circle.borderColor.cgColor;
         
        }else if (self.renderer is CornerRendener) {
            let corder = self.renderer as! CornerRendener;
            self.layer.cornerRadius = corder.redius;
            self.layer.masksToBounds = true;
        }
        
        if self.placeHolder.isEmpty  {
            self.kf.setImage(with: URL(string: self.url), placeholder: UIImage(named: self.placeHolder), options: nil, progressBlock: nil, completionHandler: { (img, error, cacheType, imageURL) in
               self.setInterceptImage(image:img!);
            })

        }else{
            self.kf.setImage(with: URL(string: self.url), placeholder: nil, options: nil, progressBlock: nil, completionHandler: { (img, error, cacheType, imageURL) in
                self.setInterceptImage(image:img!);
            })
            
            
            setInterceptImage(image: self.image!);
        }
    }

    
    func setInterceptImage(image:UIImage) {
        var interceptImage = UIImage();
        for hander in interceptors {
            if (hander is CutInterceptor) {
                let h = (hander as! CutInterceptor)
                interceptImage = UIImage.cutOut(image: image, rect: h.cutRect);
            }
            if (hander is BlurInterceptor) {
                let h = (hander as! BlurInterceptor)
                interceptImage = UIImage.blurFilter(img: image, value: h.alpha);
            }
            if (hander is ResizeInterceptor) {
                let h = (hander as! ResizeInterceptor)
                interceptImage = UIImage.resSize(image: image, size: h.size);
            }
        }
        self.image = interceptImage;
    }
}

public protocol Renderer {
//    func process(img: UIImage) -> UIImage;

}

open class CircleRenderer: Renderer {
    public var borderWidth = NSInteger();
    public var borderColor = UIColor();
    public init() {
    
    }
}

open class CornerRendener: Renderer {
    public var redius = CGFloat();
    public init() {
        
    }
}

public protocol Interceptor {
//    func process(img: UIImage) -> UIImage;
}

open class CutInterceptor: Interceptor {
    public var cutRect = CGRect();
    public init() {
        
    }
}

open class BlurInterceptor: Interceptor {
   public var alpha = CGFloat();
    public init() {
        
    }
}

open class ResizeInterceptor: Interceptor {
   public var size = CGSize();
    public init() {
        
    }
}
